#ifndef _iap_h
#define _iap_h

#include "main.h"

#define ENABLE_IRQ()    {__enable_irq();}
#define DISABLE_IRQ()   {__disable_irq();}

#define GD32_FLASH_SIZE     128     // FLASH容量大小(单位为K)

#if GD32_FLASH_SIZE < 256
#define GD_SECTOR_SIZE  1024
#else 
#define GD_SECTOR_SIZE  2048
#endif	

#define FLASH_SECTOR_SIZE   2048
#define LOAD_APP_FLAG        		((uint32_t)0xA5A5A5A5)
#define GD32_FLASH_BASE_ADR         0x08000000
#define APP_START_ADDR              0x08004000
#define IAP_FLAG_ADR		        APP_START_ADDR - GD_SECTOR_SIZE


typedef  void (*iapfun)(void);				//定义一个函数类型的参数.

typedef __IO uint32_t  vuint32_t;
typedef __IO uint16_t vuint16_t;
typedef __IO uint8_t  vuint8_t;

void WFI_SET(void);		//执行WFI指令
void INTX_DISABLE(void);//关闭所有中断
void INTX_ENABLE(void);	//开启所有中断
void MSR_MSP(uint32_t addr);	//设置堆栈地址

void iap_load_app(uint32_t addr);  //跳转到指定地址执行代码
void write_iap_flag(uint32_t addr, uint16_t data);
uint16_t fmc_read_halfword(uint32_t addr);
void iap_softreset(void);

/**
 * @brief 写FLASH
*/
void FLASH_Write(uint32_t adr, uint32_t *buf, uint16_t len);

/**
 * @brief 读FLASH
*/
void FLASH_Read(uint32_t adr, uint32_t *buf, uint16_t len);

void iap_task(void);
#endif
